//
//  OpertaionBlock.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/7/10.
//  Copyright © 2023 moon. All rights reserved.
//

#import "OpertaionBlock.h"

@implementation OpertaionBlock
+ (OpertaionBlock *)block:(void(^_Nullable)(id context))block context:(nullable id)context{
    OpertaionBlock *obj = [[OpertaionBlock alloc] init];
    obj.block = block;
    obj.contenxt = context;
    return obj;
}
- (void)run{
    if(self.block){
        self.block(self.contenxt);
    }
}
+ (void)enumBlocks:(NSArray<NSArray<OpertaionBlock *> *> *)blocks block:(void(^)(NSArray<OpertaionBlock *> *blocks))b{
    if(blocks.count==0) return;
    if(blocks.count==1){
        for(OpertaionBlock *block in blocks.firstObject){
            if(b){
                b(@[block]);
            }
        }
        return;
    }
    for(int i=0;i<blocks.count;i++){
        NSArray<OpertaionBlock *> *list = blocks[i];
        NSArray *left = [blocks subarrayWithRange:NSMakeRange(i+1, blocks.count-i-1)];
        for(OpertaionBlock *block in list){
            [self enumBlocks:left block:^(NSArray<OpertaionBlock *> *blocks) {
                if(b){
                    NSMutableArray *tmp = [[NSMutableArray alloc] init];
                    [tmp addObject:block];
                    [tmp addObjectsFromArray:blocks];
                    b(tmp);
                }
            }];
        }
    }
}
@end
